﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataModel;

namespace FriendsContactWeb
{
    public partial class Messages : System.Web.UI.Page
    {
        const int MSG_ID_COL_NO = 1;
        const int EVENT_ID_COL_NO = 5;
        const int WAS_READ_COL_NO = 8;

        DataClass dataClass = null;
        List<UserMessages> userMessages = null;
        FriendsEvent selectedEvent = null;
        int selectedMsgId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            dataClass = new DataClass(Page.User.Identity.Name);
            try
            {
                GetDataForGridView();
                CustomizeGridView();
            }
            catch(Exception ex)
            {
                ProcessException(ex);
            }
        }

        private void CustomizeGridView()
        {
            if (this.eventGridView.Rows.Count == 0)
                return;

            foreach (GridViewRow row in this.eventGridView.Rows)
            {
                if (Boolean.Parse(row.Cells[WAS_READ_COL_NO].Text) == false)
                {
                    row.Style.Add(HtmlTextWriterStyle.Color, "#CC0000");
                }
                else
                {
                    row.Style.Clear();
                }
            }
        }

        private void GetDataForGridView()
        {
            if (dataClass == null)
                return;

            userMessages = dataClass.GetMessagesForUser(Page.User.Identity.Name);
            eventGridView.DataSource = userMessages;
            eventGridView.DataBind();
        }

        protected void eventGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                e.Row.Cells[MSG_ID_COL_NO].Visible = false;
                e.Row.Cells[EVENT_ID_COL_NO].Visible = false;
                e.Row.Cells[WAS_READ_COL_NO].Visible = false;
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        protected void eventGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = this.eventGridView.SelectedIndex;
            int selectedEventId = 0;
        
            try
            {
                if (Int32.TryParse(this.eventGridView.Rows[selectedIndex].Cells[EVENT_ID_COL_NO].Text, out selectedEventId))
                {
                    selectedEvent = dataClass.GetEventFromId(selectedEventId);
                    Session["SelectedEvent"] = selectedEvent;

                    if (selectedEvent != null)
                    {
                        this.eventDataPanel.Visible = true;
                        this.titleTextBox.Text = selectedEvent.Title;
                        this.authorTextBox.Text = selectedEvent.UserName;
                        this.eventDescTextBox.Text = selectedEvent.Note;
                    }
                }
                else
                {
                    this.eventDataPanel.Visible = false;
                    this.titleTextBox.Text = "";
                    this.authorTextBox.Text = "";
                    this.eventDescTextBox.Text = "";
                }

                if (Int32.TryParse(this.eventGridView.Rows[selectedIndex].Cells[MSG_ID_COL_NO].Text, out selectedMsgId))
                {
                    dataClass.MarkMessageAsRead(selectedMsgId);
                    Session["SelectedMsgId"] = selectedMsgId;
                }
            }
            catch (Exception ex)
            {
                ProcessException(ex);
            }
        }

        protected void eventGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                decimal deletedMsgId = Decimal.Parse(e.Values["Id"].ToString());
                dataClass.DeleteMessage(deletedMsgId);
                GetDataForGridView();
            }
            catch (Exception ex)
            {
                ProcessException(ex);
            }
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            try
            {
                if (selectedEvent == null)
                {
                    selectedEvent = Session["SelectedEvent"] as FriendsEvent;
                }
                dataClass.AddNewEventFromFeedback(selectedEvent.Id, Page.User.Identity.Name);
                
                //creates new feedback if it does not exist; 
                //otherwise overvrites existing item with note and isAccepted flag
                dataClass.SendFeedback(selectedEvent.Id, Page.User.Identity.Name, true, 
                                       this.eventDescTextBox.Text);

                if (selectedMsgId <= 0)
                {
                    selectedMsgId = (int)Session["SelectedMsgId"];
                }
                dataClass.MarkMessageFromFeedback(selectedMsgId, true);
                
                GetDataForGridView();
            }
            catch (Exception ex)
            {
                ProcessException(ex);
            }
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            //todo
            //Send feedback to author +

            try
            {
                if (selectedEvent == null)
                {
                    selectedEvent = Session["SelectedEvent"] as FriendsEvent;
                }
                dataClass.SendFeedback(selectedEvent.Id, Page.User.Identity.Name, false, "");

                if (selectedMsgId == 0)
                {
                    selectedMsgId = (int)Session["SelectedMsgId"];
                }
                dataClass.MarkMessageFromFeedback(selectedMsgId, false);

                GetDataForGridView();
            }
            catch (Exception ex)
            {
                ProcessException(ex);
            }
        }

        private void ProcessException(Exception ex)
        {
            if (Request.IsLocal)
            {
                Session["CurrentError"] = ex.Message;
            }
            else
            {
                Session["CurrentError"] = "Error processing page.";
            }
            Server.Transfer("ErrorPage.aspx");
        }


    }
}
